import numpy as np

def calculate_local_disagreement(adj_row, v_i, v_matrix):
    """
    计算单个节点的局部不一致性 (local disagreement, DG_i)
    :param adj_row: 邻接矩阵中对应节点的行，代表该节点的邻居连接情况
    :param v_i: 当前节点的观点值
    :param v_matrix: 所有节点的观点矩阵
    :return: 该节点的局部不一致性 DG_i
    """
    neighbor_indices = np.where(adj_row != 0)[0]
    num_neighbors = len(neighbor_indices)
    if num_neighbors == 0:
        return 0
    diff_squared_sum = np.sum((v_i - v_matrix[neighbor_indices]) ** 2)
    return diff_squared_sum / num_neighbors

def calculate_global_disagreement(adj_matrix, v_matrix):
    """
    计算全局不一致性 (Global Disagreement, DG)
    :param adj_matrix: 邻接矩阵，表示网络结构
    :param v_matrix: 所有节点的观点矩阵
    :return: 全局不一致性 DG
    """
    num_nodes = adj_matrix.shape[0]
    num_rounds = v_matrix.shape[1]
    global_disagreements = []
    for r in range(num_rounds):
        local_disagreements = []
        for i in range(num_nodes):
            v_i = v_matrix[i][r]
            adj_row = adj_matrix[i]
            local_dg = calculate_local_disagreement(adj_row, v_i, v_matrix[:, r])
            local_disagreements.append(local_dg)
        # 根据新公式计算每轮DG
        dg = (1 / (2 * num_nodes)) * np.sum(local_disagreements)
        global_disagreements.append(dg)
    return np.array(global_disagreements)

# 正确示例：3 节点，观点矩阵形状 (3, 5)
adj_matrix = np.array([
        [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
        [0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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1, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1],
        [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
])
#v_matrix_2d = np.array([[[0.04, 0.01, 0.03, 0.67, 0.99], [0.89, 0.74, 0.83, 0.8, 0.2], [0.26, 0.14, 0.76, 0.59, 0.14], [0.81, 0.39, 0.58, 0.99, 0.74], [0.9, 0.47, 0.11, 0.98, 0.54], [0.0, 0.53, 0.59, 0.61, 0.5], [0.28, 0.29, 0.96, 0.63, 1.0], [0.8, 0.01, 0.12, 0.45, 0.44], [0.86, 0.73, 0.33, 0.27, 0.82], [0.28, 0.2, 0.42, 0.96, 0.61], [1.0, 0.97, 0.36, 0.37, 1.0], [0.48, 0.98, 0.88, 0.72, 0.1], [0.51, 0.6, 0.3, 0.45, 0.78], [0.17, 0.38, 0.36, 0.32, 0.86], [0.24, 0.96, 0.48, 0.56, 0.81], [0.91, 0.71, 0.27, 0.09, 0.46], [0.45, 0.43, 0.13, 0.71, 0.89], [1.0, 0.03, 0.24, 0.74, 0.66], [0.25, 0.93, 0.98, 0.66, 0.57], [0.9, 0.3, 0.29, 0.3, 0.86], [0.26, 0.57, 0.33, 0.2, 0.62], [0.57, 0.48, 0.4, 0.84, 0.18], [0.19, 0.46, 0.65, 0.26, 0.34], [0.57, 0.64, 0.06, 0.14, 0.01], [0.22, 0.84, 0.73, 0.4, 0.03], [0.76, 0.57, 0.19, 0.49, 0.4], [0.3, 0.97, 0.77, 0.92, 0.95], [0.61, 0.74, 0.31, 0.54, 0.56], [0.4, 0.7, 0.71, 0.74, 0.98], [0.2, 0.55, 0.2, 0.94, 1.0]]])

v_matrix_2d = np.array([
    [0.04, 0.01, 0.03, 0.67, 0.99],
    [0.89, 0.74, 0.83, 0.8, 0.2],
    [0.26, 0.14, 0.76, 0.59, 0.14],
    [0.81, 0.39, 0.58, 0.99, 0.74],
    [0.9, 0.47, 0.11, 0.98, 0.54],
    [0.0, 0.53, 0.59, 0.61, 0.5],
    [0.28, 0.29, 0.96, 0.63, 1.0],
    [0.8, 0.01, 0.12, 0.45, 0.44],
    [0.86, 0.73, 0.33, 0.27, 0.82],
    [0.28, 0.2, 0.42, 0.96, 0.61],
    [1.0, 0.97, 0.36, 0.37, 1.0],
    [0.48, 0.98, 0.88, 0.72, 0.1],
    [0.51, 0.6, 0.3, 0.45, 0.78],
    [0.17, 0.38, 0.36, 0.32, 0.86],
    [0.24, 0.96, 0.48, 0.56, 0.81],
    [0.91, 0.71, 0.27, 0.09, 0.46],
    [0.45, 0.43, 0.13, 0.71, 0.89],
    [1.0, 0.03, 0.24, 0.74, 0.66],
    [0.25, 0.93, 0.98, 0.66, 0.57],
    [0.9, 0.3, 0.29, 0.3, 0.86],
    [0.26, 0.57, 0.33, 0.2, 0.62],
    [0.57, 0.48, 0.4, 0.84, 0.18],
    [0.19, 0.46, 0.65, 0.26, 0.34],
    [0.57, 0.64, 0.06, 0.14, 0.01],
    [0.22, 0.84, 0.73, 0.4, 0.03],
    [0.76, 0.57, 0.19, 0.49, 0.4],
    [0.3, 0.97, 0.77, 0.92, 0.95],
    [0.61, 0.74, 0.31, 0.54, 0.56],
    [0.4, 0.7, 0.71, 0.74, 0.98],
    [0.2, 0.55, 0.2, 0.94, 1.0]
])  # 形状 (30, 5)


# 转置操作（行列交换）

print("原始形状:", v_matrix_2d.shape)  # 输出 (30,5)
# 选择要清零的列（例如第3列，索引2）
column_index = 4
v_matrix_2d[:, column_index] = 0

g=calculate_global_disagreement(adj_matrix,v_matrix_2d)
print("神经质为1:",g)
